# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: 异步调用和回调机制.py
Author: kij
Create Date: 2021/3/20
-------------------------------------------------
"""
from concurrent.futures import ThreadPoolExecutor
import time
import random


# 异步调用就是，启动之后 等结果出来自动通知你结果 。不用等待,可以执行其他的方法
def func1(name):
	print('func1%s' % name)
	time.sleep(random.randint(3, 5))
	res = random.randint(7, 13) * '#'
	return {'name': name, 'res': res}


def func2(weigh):
	weigh = weigh.result()
	name = weigh['name']
	size = len(weigh['res'])
	print('%s 重量 = %s kg' % (name, size))


if __name__ == '__main__':
	pool = ThreadPoolExecutor(13)
	# # 下面同步调用
	# weigh = pool.submit(func1,'kij').result()
	# func2(weigh)
	# weigh2 = pool.submit(func1,'kijlee').result()
	# func2(weigh2)
	# weigh3 = pool.submit(func1,'kij_lee').result()
	# func2(weigh3)
	# 下面同步调用
	pool.submit(func1, 'kij').add_done_callback(func2)
	pool.submit(func1, 'kijlee').add_done_callback(func2)
	pool.submit(func1, 'kij_lee').add_done_callback(func2)
